/**
 * Script to handle displaying all of the forms in the browser
 * This is the main Application pseudo-class
 *
 * Author: Chris Casola
 */

(function() {
	
	/**
	 * Displays the application in the browser
	 */
	function Frame() {
		
		/** The form where the user enters the question */
		this.questionForm = new QuestionForm(this);
		
		/** THe form where users an sign in to an existing event */
		this.signInForm = new SignInForm(this);
		
		/** The form used to create a closed event */
		this.createClosedForm = new CreateClosedEventForm(this);
		
		/** The form used to create an open event */
		this.createOpenForm = new CreateOpenEventForm(this);
		
		/** The form containing the create event button */
		this.createButtonForm = new CreateButtonForm(this);
		
		/** The form used to add choices to an open event */
		this.addChoiceForm = new AddChoiceForm(this);
		
		/** The form containing the add edge canvas */
		this.addEdgeForm = new AddEdgeForm(this);
		
		/** The form currently being displayed */
		window.currentForm = "start";
		

		/**
		 * Contents of the tool tips
		 */
		window.toolTips = {
			'eventType' : 'A closed event has a specified number of participants and is not available for the public to join. An open event has a flexible number of participants and allows anyone to join.',
			'peopleTip' : 'You must specify the number of people participating in a closed event.',
			'openRoundsTip' : 'This is the number of times each participant will have a chance to add an Edge.',
			'closedRoundsTip' : 'This is the number of times each participant will have a chance to add an Edge.',
			'maxChoicesTip' : 'This is the maximum number of choices that can be provided by participants in an open event.',
			'joinOptions' : 'Enter your choice and click \"Join Event\" to join the event.  You may also click \"Forfeit Choice\" if you do not with to enter your own choice.',
			'signInTip' : 'Enter the game ID you were provided with and choose a name and password to sign in with.',
		};

		/**
		 * Change the form currently being displayed on the page
		 */
		this.changeForm = function(formName) {
			var mainContent = document.getElementById('mainContent');
			
			// display only the question form when the page loads
			if (formName == 'start') {
				mainContent.innerHTML = this.questionForm.displayForm() + this.createClosedForm.displayForm() + this.createOpenForm.displayForm() + this.createButtonForm.displayForm() + this.addEdgeForm.displayForm() + this.addChoiceForm.displayForm() + this.signInForm.displayForm();
				document.getElementById('btnClosed').addEventListener("click", function() {window.frame.changeForm("closed")}, false);
				document.getElementById('btnOpen').addEventListener("click", function() {window.frame.changeForm("open")}, false);
				document.getElementById('createEvent').addEventListener("click", function() {CreateEventController(window.currentForm)}, false);
				document.getElementById('addChoice').addEventListener("click", addChoiceController, false);
				document.getElementById('removeChoice').addEventListener("click", removeChoiceController, false);
				document.getElementById('submitChoice').addEventListener('click', AddChoiceController, false);
				document.getElementById('btnShowSignIn').addEventListener('click', function() {window.frame.changeForm("signIn")}, false);
				document.getElementById('signIn').addEventListener('click', SignInController, false);
				window.currentForm = "start";
			}
			// add the closed form when the closed button is clicked 
			else if (formName == "closed" && window.currentForm != "closed") {
				window.currentForm = "closed";
				document.getElementById('closedInputForm').style.display = "block";
				document.getElementById('createEventButtonPanel').style.display = "block";
				document.getElementById('openInputForm').style.display = "none";
			}
			// add the open form when the open button is clicked
			else if (formName == "open" && window.currentForm != "open") {
				window.currentForm = "open";
				document.getElementById('openInputForm').style.display = "block";
				document.getElementById('createEventButtonPanel').style.display = "block";
				document.getElementById('closedInputForm').style.display = "none";
			}
			// the sign in form
			else if (formName == "signIn") {
				window.currentForm = "signIn";
				document.getElementById('openInputForm').style.display = "none";
				document.getElementById('createEventButtonPanel').style.display = "none";
				document.getElementById('closedInputForm').style.display = "none";
				document.getElementById('createForm').style.display = "none";
				document.getElementById('signInForm').style.display = "block";
			}
			// show the addEdge form
			else if (formName == "addEdge") {
				window.currentForm == "addEdge";
				document.getElementById('createForm').style.display = "none";
				document.getElementById('openInputForm').style.display = "none";
				document.getElementById('createEventButtonPanel').style.display = "none";
				document.getElementById('closedInputForm').style.display = "none";
				document.getElementById('signInForm').style.display = "none";
				document.getElementById('addChoiceForm').style.display = "none";
				document.getElementById('addEdgeForm').style.display = "block";
				this.addEdgeForm.initCanvas();
				this.addEdgeForm.initChoiceTable();
				updateTurnStatus();
			}
			// show the addChoice form
			else if (formName == "addChoice") {
				window.currentForm = "addChoice";
				document.getElementById('signInForm').style.display = "none";
				document.getElementById('createForm').style.display = "none";
				document.getElementById('openInputForm').style.display = "none";
				document.getElementById('createEventButtonPanel').style.display = "none";
				document.getElementById('closedInputForm').style.display = "none";
				document.getElementById('addEdgeForm').style.display = "none";
				document.getElementById('addChoiceForm').style.display = "block";
				document.getElementById('addChoiceForm').children[0].innerHTML = window.dlEvent.question;
				if (sessionStorage['isModerator'] == 'true') {
					var choiceBtnList = document.getElementById('choiceFormButtons');
					var newBtn = document.createElement('li');
					newBtn.setAttribute('class', 'button');
					newBtn.setAttribute('id', 'btnForceClose');
					newBtn.innerHTML = "Force Close";
					newBtn.addEventListener("click", ForceCloseController, false);
					choiceBtnList.appendChild(newBtn);
				}
			}
			// show the result
			else if (formName == "showResult") {
				var strResult = window.dlEvent.getPrioritizedOrder();
				var resultList = "";
				for (var i=0; i < strResult.length; i++) {
					resultList += "<li>" + strResult[i] + "</li>";
				}
				document.getElementById('mainContent').innerHTML = "<h1>Prioritized Order</h1><ol id='resultList'>" + resultList + "</ol>";
			}
		};
	}

	window.Frame = Frame;
}).call();

/**
 * The onload function that initializes the page
 */
window.onload = function() {
	window.frame = new Frame(); /* construct the frame that contains the entire HTML contents of the page */
	window.frame.changeForm("start"); /* display the start form, which is the question box and open, close buttons */
	addToolTipListeners(); /* enable the tool tips popups */
	sessionStorage['isModerator'] = false; /* default to being a non-moderator */
	
	/**
	 * The Event holding all of the game state. Adding it to
	 * the window makes it globally accessible
	 */
	window.dlEvent = {};
	
	/**
	 * The commmunicator - used to send and receive messages
	 * from the server
	 */
	window.comm = new Communicator();
	window.comm.onmessage = parseResponse;
	
	/**
	 * Call back functions for Communicator that are used for
	 * debugging purposes, they make log entries to the console.
	 */
	window.comm.onerror = function(jq, etype) {
		console.debug("Response: " + JSON.stringify(jq) + "   Error Type:" + etype);
	};
	
	window.comm.onsend = function(request) {
		console.debug("Just sent this request: " + JSON.stringify(request));
	};
	
	comm.onkeepalive = function() {
		console.debug("Sending Keepalive");
	};
	
	comm.ontimeout = function() {
		console.debug("Timeout");
	};
	
	window.comm.start();
	
	/**
	 * Parse the gameid from the URL if it is provided
	 */
	var urlRegEx = /\S*\?(\S*)/;
	var gameid;
	if (gameid = urlRegEx.exec(window.location.href)) {
		window.frame.changeForm("signIn");
		document.getElementById('gameID').value = gameid[1]; 
	}
};
